package popple.cameras
{
	import com.adobe.utils.PerspectiveMatrix3D;
	
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Vector3D;
	
	import popple.display.DisplayObject;
	import popple.display.Stage;


	public class Camera
	{
		static public var current:Camera;
		protected var mPerspectiveMtx:PerspectiveMatrix3D;
		protected var mTarget:DisplayObject;
		protected var mOutMatrix:Matrix3D=new Matrix3D;
		protected var mPosition:Vector3D=new Vector3D(-1,Stage.scale,0);
		
		protected var mWatchMatrix:Matrix3D;
		public function Camera()
		{
			mPerspectiveMtx=new PerspectiveMatrix3D;
			//1.056
			//mPerspectiveMtx.perspectiveFieldOfViewLH(1.058,Stage.width/Stage.height,.1,2);
			mPerspectiveMtx.perspectiveLH(1,1*Stage.scale,.5,10000);
		}
		public function watch(obj:DisplayObject,matrix:Matrix3D):void
		{
			mTarget=obj;
			mWatchMatrix=matrix;
		}
		public function set active(value:Boolean):void
		{
			if(value==true)
			{
				current=this;
			}
		}
		/**
		 * 相机位置
		 * @param pos
		 * 
		 */		
		public function set position(pos:Vector3D):void
		{
			mPosition=pos;
		}
		/**
		 *获取相机数据 
		 * @return 
		 * 
		 */		
		public function capture():Matrix3D
		{
			mOutMatrix.identity();
			if(mTarget!=null)
			{
				var tm:Vector3D=mTarget.position;
				  
				mOutMatrix.appendTranslation(-tm.x,-tm.y,-tm.z);
				mOutMatrix.append(mWatchMatrix);
			}
			else
			{
				mOutMatrix.appendTranslation(mPosition.x,mPosition.y,mPosition.z);
			}
			
			mOutMatrix.append(mPerspectiveMtx);
			return mOutMatrix;
		}
		public function get target():DisplayObject
		{
			return mTarget;
		}
	}
}